<?php
/**
 * Created by PhpStorm.
 * User: zhd
 * Date: 2019/3/29
 * Time: 22:49
 */

namespace app\Module\Index\Controller;

use app\Module\Index\Model\ArticleModel;

class ArticleCollectController extends CheckAuth
{
    /**
     * 获取文章收藏列表
     *
     * @verify int|null $page
     * @verify int|null $limit
     * @return string
     */
    public function getListAction($page = 1, $limit = 10)
    {
        $articleModel = new ArticleModel();
        $articleModel->setFields("article.*,user.username,user.avatar_url,user.latitude,user.longitude,user.address_name,one_type.name as one_type_name,one_type.id as one_type_id,GROUP_CONCAT(DISTINCT two_type.name) as two_type_name,GROUP_CONCAT(DISTINCT two_type.id) as two_type_id,GROUP_CONCAT(DISTINCT article_image.url) as images,article_video.url as video");
        $articleModel->setJoin('article_type', 'article.id= article_type.article_id');
        $articleModel->setJoin('one_type', 'one_type.id= article_type.one_type_id');
        $articleModel->setJoin('two_type', 'two_type.id= article_type.two_type_id');
        $articleModel->setJoin('user', 'user.id= article.user_id');
        $articleModel->setJoin('article_collect', 'article.id= article_collect.article_id');
        $articleModel->setJoin('article_image', 'article_image.article_id= article.id', 'LEFT JOIN');
        $articleModel->setJoin('article_video', 'article_video.article_id= article.id', 'LEFT JOIN');
        $articleModel->setPage($page, $limit);
        $articleModel->addOrder('article_collect.created', false);
        $articleModel->addWhere([
            'article_collect.user_id' => session('userId'),
            'article.is_hide'         => 0,
            'one_type.is_hide'        => 0,
            'two_type.is_hide'        => 0,
            'article.is_del'          => 0,
        ]);
        $articleModel->addGroup('article.id');
        $data = $articleModel->select();
        return success($data);
    }
}